package com.tianji.learning.mq;

import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.messsge.SignMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.SIGN_IN;
import static com.tianji.common.constants.MqConstants.Key.WRITE_REPLY;
import static com.tianji.common.constants.MqConstants.Queue.QA_POINTS_QUEUE;
import static com.tianji.common.constants.MqConstants.Queue.SIGN_POINTS_QUEUE;

@Slf4j
@Component
@RequiredArgsConstructor
public class LisenerSignRecordMsg {
    private final IPointsRecordService recordService;

    /**
     * 学习问答积分消息监听
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QA_POINTS_QUEUE, durable = "true"),
            exchange = @Exchange(value = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = WRITE_REPLY)
    )
    public void addPointsRecords(Long userId) {

        log.info("接收到");
        recordService.addPointsRecords(userId, 5, PointsRecordType.QA);
    }

    /**
     * 签到积分消息监听
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = SIGN_POINTS_QUEUE, durable = "true"),
            exchange = @Exchange(value = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = SIGN_IN)
    )
    public void addPointsRecords(SignMessage message) {
        recordService.addPointsRecords(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
    }
}
